package com.enation.app.web.core.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.enation.app.web.core.model.SpiteVisitor;
import com.enation.app.web.core.service.ISpiteVisitorManager;
import com.enation.framework.database.IDaoSupport;
import com.enation.framework.database.Page;
import com.enation.framework.util.DateUtil;

/**
 * 
 * @function 恶意访问实现
 * @author 闯哥
 * @time 2016年12月11日 下午1:41:50
 */
@Service
public class SpiteVisitorManager implements ISpiteVisitorManager {

	@Autowired
	private IDaoSupport daoSupport;

	@Override
	public void insert(SpiteVisitor spiteVisitor) {

		this.daoSupport.insert("es_spitevisitor", spiteVisitor);

	}

	@Override
	public Page findList(Map<String, Object> result, Integer pageNo,
			Integer pageSize) {

		StringBuffer sql = new StringBuffer(
				"select * from es_spitevisitor where 1=1");

		// 学号
		String student_number = (String) result.get("student_number");
		if (student_number != null) {
			if (!student_number.equals("")) {
				sql.append(" and student_number='" + student_number + "'");
			}
		}

		// 真实姓名
		String real_name = (String) result.get("real_name");
		if (real_name != null) {
			if (!real_name.equals("")) {
				sql.append(" and real_name='" + real_name + "'");
			}
		}

		// 访问页面
		String pageName = (String) result.get("pageName");
		if (pageName != null) {
			if (!pageName.equals("") && !pageName.equals("0")) {
				sql.append(" and pageName='" + pageName + "'");
			}
		}

		// 访问时间
		String start_time = (String) result.get("start_time");
		if (start_time != null) {
			if (!start_time.equals("")) {
				Date start_date = DateUtil.toDate(start_time, "yyyy-MM-dd");
				Long start = start_date.getTime() / 1000;
				sql.append(" and time>" + start);
			}
		}

		String end_time = (String) result.get("end_time");
		if (end_time != null) {
			if (!end_time.equals("")) {
				Date end_date = DateUtil.toDate(end_time, "yyyy-MM-dd");
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(end_date);
				calendar.add(Calendar.DAY_OF_YEAR, 1);
				end_date = calendar.getTime();
				Long end = end_date.getTime() / 1000;
				sql.append(" and time<" + end);
			}
		}

		sql.append(" order by time desc");

		Page page = this.daoSupport.queryForPage(sql.toString(), pageNo,
				pageSize, SpiteVisitor.class);
		return page;
	}

	@Override
	public List<String> findPageName() {

		String sql = "select pageName from es_spitevisitor group by pageName";
		Map<String, Object> pageNamesMap = this.daoSupport.queryForMap(sql);
		List<String> pageNames = new ArrayList<String>();
		for (String key : pageNamesMap.keySet()) {
			pageNames.add(pageNamesMap.get(key).toString());
		}
		return pageNames;
	}

}
